/*
 *      Author: Guannan Ma
 *      @mythmgn
 *      Whatever license applied
 */

#include <stdlib.h>
#include <string.h>

#include "base.h"

hTab *hcf=NULL;

/*
static char* ParamServer[]= {
    "1WASD_TOP",
    "1WASD_PID",
    "1WASD_CONF"
    "1WASD_LOGDIR",
    "1WASD_LOG_LEVEL",
    "1WASD_SERVER_LISTEN_PORT",
    "1PLUGIN_ENABLE",
    "1WASD_RUN_USERS"
};
*/



BOOL
initConf(char *topDIR) {
    char line[MAX_LENGTH_OF_LINE];
    char key[MAX_LENGTH_OF_CONF_KEY];
    char value[MAX_LENGTH_OF_CONF_VALUE];

    FILEW fconf;
    lineInfo li;
    conf_param_item *cpi; /*conf item value*/

    fconf.fp=NULL;
    memset(fconf.fname,'\0',sizeof(fconf.fname));

    li.lineNum=1;
    cpi=NULL;


    memset(line,'\0',sizeof(line));
    memset(key,'\0',sizeof(key));
    memset(value,'\0',sizeof(value));


    if (FALSE==(hcf=initHashTable(CONF_PARAM_NUMBER))) {
        goto freeup;
    }

    if (NULL==strcpy(fconf.fname,topDIR))/*||NULL==strcat(fconf.fname,"/conf/wasd.conf.work"))*/
        goto freeup;
    if (NULL==(fconf.fp=fopen(fconf.fname,"r")))
        goto freeup;


    while (readLine(&fconf,line,&li)) {
        if (isContainSign(line)) {
            memset(line,'\0',MAX_LENGTH_OF_LINE);
            continue;
        }
        memset(key,'\0',MAX_LENGTH_OF_CONF_KEY);
        memset(value,'\0',MAX_LENGTH_OF_CONF_VALUE);

        if (FALSE==getKeyAndValue(line,key,value))
            goto freeup;

        if (NULL==(cpi=(conf_param_item *)malloc(sizeof(conf_param_item))))
            goto freeup;
        if (NULL==(cpi->key=(char *)malloc(sizeof(char)*MAX_LENGTH_OF_CONF_KEY)))
            goto freeup;
        if (NULL==(cpi->value=(char *)malloc(sizeof(char)*MAX_LENGTH_OF_CONF_VALUE)))
            goto freeup;

        memset(cpi->key,'\0',MAX_LENGTH_OF_CONF_KEY);
        memset(cpi->value,'\0',MAX_LENGTH_OF_CONF_VALUE);
        strcpy(cpi->key,key);
        strcpy(cpi->value,value);

        if (FALSE==putHashItem(hcf,key,cpi))
            goto freeup;

        memset(line,'\0',MAX_LENGTH_OF_LINE);

    }

    return TRUE;


freeup:
    if(cpi) {
        FREEUP(cpi->key);
        FREEUP(cpi->value);
        FREEUP(cpi);
    }
    closeConf(hcf);
    return FALSE;
}


/*BOOL
checkConfServer(hTab *hconf) {
    int i;
    char *temp;
    if (NULL==hconf)
        return FALSE;
    for (i=0; i<CONF_PARAM_NUMBER; i++) {
        if (0==ParamServer[i][0])
            continue;
        temp=&(ParamServer[i][1]);
        if (NULL==getHashItem(hconf,temp))
            return FALSE;
    }
    return TRUE;

}
BOOL
checkConfPlugin(hTab *hcf) {

	return TRUE;
}
*/

void
freeConfItem(void *content) {
    conf_param_item* cf=(conf_param_item *)content;
    FREEUP(cf->key);
    FREEUP(cf->value);
}

BOOL
closeConf(hTab *hconf) {
    return freeHashTable(hconf,TRUE,freeConfItem);
}

hTab *getCurrentConfHtab() {
    return hcf;
}

BOOL setCurrentConfHtab(hTab *ph) {
    if (NULL!=ph) {
        hcf=ph;
        return TRUE;
    } else {
        return FALSE;
    }

}

char *getConfItem(const char *key){
    hTab *p_htab=getCurrentConfHtab();

    if(NULL!=p_htab){
        return (char *)getHashItem(p_htab,key);
    }
    else{
        setErrStatusCode(ITEM_NOT_FOUND);
        return NULL;
    }
}
